<?php

namespace App\Admin\Controllers;

use App\Admin\Libs\Zip;
use Dcat\Admin\Traits\HasUploadedFile;

class ImageController
{

    use HasUploadedFile;

    public function handle()
    {
        $disk = $this->disk('admin');

        // 判断是否是删除文件请求
        if ($this->isDeleteRequest()) {
            // 删除文件并响应
            return $this->deleteFileAndResponse($disk);
        }

        // 获取上传的文件
        $file = $this->file();

        // 获取上传的字段名称
        $column = $this->uploader()->upload_column;

        $dir = 'images';
        $newName = $column.'-'.uniqid().'.'.$file->getClientOriginalExtension();
        $path = "{$dir}/$newName";

    $imgzip = new Zip($file,1);
    $imgzip->compressImg($path);

        return $imgzip
            ? $this->responseUploaded($path, $disk->url($path))
            : $this->responseErrorMessage('文件上传失败');

    }
}
